<?php
/**
 * @author GhostWei
 * @date Sep 9, 2012
 */
class controller_www_account_setting extends front_controller_uc {	
	
	public function action_index() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('修改资料'))->setRoute('account/setting/index'));
		if($this->getRequest()->isPost()) {
			$row = $this->user;
			$row->setNickname(trim($this->_post('nickname')));
			$row->setGender(intval($this->_post('gender')));
			$row->setMemo($this->_post('memo'));
			$this->_saveToDatabase($row, 'avatar', 'avatar');
			$this->view->user = $row;
		}
		$this->render();
	}	
	
	public function action_password() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('修改密码'))->setRoute('account/setting/password'));
		if($this->getRequest()->isPost()) {
			$this->view->referer = $this->referer = $this->_post('referer');
			try {
				$p1 = trim($this->_post('password1'));
				$p2 = trim($this->_post('password2'));
				$p3 = trim($this->_post('password'));
				if(md5($p1) != $this->user->getCredential()) {
					throw new front_exception("原始密码不正确");
				}
				if(strlen($p2) < 6) {
					throw new front_exception("新密码太短，至少要6个字符");
				}
				if(md5($p2) != md5($p3)) {
					throw new front_exception("两次新密码输入不一至");
				}
				$row = $this->user;
				$row->setCredential(md5($p2));
				$this->_saveToDatabase($row, 'avatar', 'avatar');
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->render();
	}
	
	public function action_email() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('修改邮箱'))->setRoute('account/setting/email'));
		if($this->getRequest()->isPost()) {
			$this->view->referer = $this->referer = $this->_post('referer');
			try {
				$p = trim($this->_post('password'));
				$this->view->email = $e1 = strtolower(trim($this->_post('email')));
				$this->view->email1 = $e2 = strtolower(trim($this->_post('email1')));
				if(md5($p) != $this->user->getCredential()) {
					throw new front_exception("您的密码不正确");
				}
				if($e1 != $e2) {
					throw new front_exception("两次邮箱地址输入不一至");
				}
				$row = $this->user;
				$row->setEmail($e1);
				$this->_saveToDatabase($row, 'avatar', 'avatar');
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->render();
	}	
	
	public function action_avatar() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('修改头像'))->setRoute('account/setting/avatar'));
		$concept = new community_member_avatar_concept();
		$this->view->list = $concept->fetchAll(array('passport_id = ?' => $this->user->getId()), array('creationtime DESC'));
		$this->render();
	}
	
	public function action_oauth() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Oauth 授权'))->setRoute('account/setting/oauth'));
		$concept = new community_oauth_concept();
		$list = array();
		foreach($concept->fetchAll(array('passport_id = ?' => $this->user->getId()), array('creationtime ASC')) AS $row) {
			$list[$row['source_id']] = $row;
		}
		$this->view->list = $list;
		$this->render();
	}	
	
}
